<?php
if (!defined('IN_D')) die("Error 404");
class Template {
	var $cache_tpl = array();
 	var $dir_tpl_name;
	var	$path_dir_cache;
	 function get_tpl_cache($name){
	 	global $conf,$func;
		$this->path_dir_cache="templates/".$this->dir_tpl_name."/tpl_cache";
		if (!is_dir($this->path_dir_cache))
	 		mkdir($this->path_dir_cache);
	 	$tpl_cache_name=$this->path_dir_cache."/".$name.".html";
		if(file_exists($tpl_cache_name))
		{
			$fpr=fopen($tpl_cache_name,"r");
			$contents = fread($fpr, filesize($tpl_cache_name));
			fclose($fpr);
			return $contents;
		}	
			return false;	
	 }
	function get_tpl($name,$blockname = '',$c = false) {
		global $conf,$DB,$lang,$func;
		$sql_tpl=$DB->query("SELECT tpl_foldername FROM {$conf['prefix']}tpl WHERE tpl_id=".$_SESSION['sescurrent_tpl']);
		$temp=$DB->fetch_row($sql_tpl);
		$this->dir_tpl_name=$temp["tpl_foldername"];		
		if(!empty($conf['use_cache_html']))
			$this->cache_tpl['tpl_'.$name]=$this->get_tpl_cache($name);
		if (!empty($this->cache_tpl['tpl_'.$name])) $content = $this->cache_tpl['tpl_'.$name];
		else {
			$sql = $DB->query("SELECT skin_value FROM ".$conf['prefix']."skin WHERE skin_tpl='".$_SESSION['sescurrent_tpl']."' AND skin_name='{$name}'");
			if (!($DB->num_rows($sql))) {
				$default_tpl = $conf['default_tpl'];
				die("Không tìm thấy template : <b>".$name."</b><br><a href='?reset_tpl=1'>Trở về giao diện mặc định</a>");
			}
				$cont=$DB->fetch_row($sql);
				$content = $func->unhtmlchars(stripslashes($cont['skin_value']));
				$this->cache_tpl['tpl_'.$name]=$content;
				$path_cache=$this->path_dir_cache."/".$name.".html";
				$fpw=fopen($path_cache,"w");
				fwrite($fpw, $content);
				fclose($fpw);
		}
		return $content;
	}
	
	function get_block_from_str($str,$block = '',$c = false) {
		
		if (empty($this->cache_tpl['block_'.$block])) {
			preg_replace('#<!-- '.(($c)?'\#':'').'BEGIN '.$block.' -->[\r\n]*(.*?)[\r\n]*<!-- '.(($c)?'\#':'').'END '.$block.' -->#se','$s = stripslashes("\1");',$str);
			if ($s != $str)	$str = $s;
			else $str = '';
			$this->cache_tpl['block_'.$block] = $str;
		}
		return $this->cache_tpl['block_'.$block];
	}
	
	function auto_get_block($str) {
		preg_match_all('#<!-- \#BEGIN (.*?) -->[\r\n]*(.*?)[\r\n]*<!-- \#END (.*?) -->#s', $str, $arr, PREG_PATTERN_ORDER);
		$a = array();
		for ($i=0; $i<count($arr[0]); $i++) {
			$a[$arr[1][$i]] = $arr[0][$i];
		}
		return $a;
	}

	function unset_block($code,$arr,$c = false) {
		foreach ($arr as $block) {
			$code = preg_replace('#<!-- '.(($c)?'\#':'').'BEGIN '.$block.' -->[\r\n]*(.*?)[\r\n]*<!-- '.(($c)?'\#':'').'END '.$block.' -->#s', '', $code);
		}
		return $code;
	}
	function assign_vars($code,$arr) {
		foreach ($arr as $block => $val) {
				$code = str_replace('{'.$block.'}',$val,$code);
		}
		return $code;
	}
	function assign_vars2($code,$arr) {
		foreach ($arr as $block => $val) {
				$code = str_replace('{'.$block.'}', $val,$code);
		}
		$code = str_replace("\"","\\\"",$code);
		return $code;
	}
	function assign_blocks_content($code,$arr) {
		foreach ($arr as $block => $val) {
			$code = preg_replace('#<!-- BEGIN '.$block.' -->[\r\n]*(.*?)[\r\n]*<!-- END '.$block.' -->#s', $val, $code);
		}
		return $code;
	}
	function parse_box($func,$exp = '') {
		$exp = trim(stripslashes($exp));
		if ($exp) $code = eval("return ".$func."(".$exp.");");
		else $code = eval("return ".$func."();");

		return $code;
	}
	
	function parse_lang($key) {
		global $lang;
		$key = trim(stripslashes($key));
		if ($key) $code = $lang[$key];
		return $code;
	}
	
	function parse_tpl($code) {
		global $conf,$lang,$DB;
		if(!$this->dir_tpl_name){
			$sql_tpl=$DB->query("SELECT tpl_foldername FROM {$conf['prefix']}tpl WHERE tpl_id=".$_SESSION['sescurrent_tpl']);
			$temp=$DB->fetch_row($sql_tpl);
			$this->dir_tpl_name=$temp["tpl_foldername"];
		}
		$code = preg_replace('#<!-- BOX (.*?)\((.*?)\) -->#se', '$this->parse_box("\\1","\\2");', $code);
		$code = preg_replace('#{\$lang\[[\'"](.*?)[\'"]\]}#se', '$this->parse_lang("\\1")', $code);
		$code = str_replace('{TPL_LINK}', $conf['web_url']."/templates/".$this->dir_tpl_name, $code);
		$code = str_replace('{WEB_TITLE}', $conf['web_title'], $code);
		$code = str_replace('{WEB_URL}', $conf['web_url'], $code);
		$code = str_replace('{TPL_STYLES}', $conf['web_url'].'/styles.php?tpl_id='.$_SESSION['sescurrent_tpl'], $code);
		$code = preg_replace('#<!-- BEGIN (.*?) -->[\r\n]*(.*?)[\r\n]*<!-- END (.*?) -->#s', '\\2', $code);
		return $code;
	}
}
?>